package com.snowwolf.user.service.mongodb.impl;

import com.snowwolf.user.dao.mongodb.NewsMdbDao;
import com.snowwolf.user.entity.mongodb.NewsMdbEo;
import com.snowwolf.user.service.mongodb.INewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author: topsnowwolf
 * @description:
 * @date: Create in 2019/3/16 9:27
 * @modified by:
 * @versions：0.1.0
 */
@Service
public class NewServiceImpl implements INewService {
    @Autowired
    private NewsMdbDao newsMdbDao;
    @Override
    public String save(NewsMdbEo newsMdbEo) {
        if(newsMdbEo.getCreateTime()==null){
            newsMdbEo.setCreateTime(new Date());
        }
        if(newsMdbEo.getUpdateTime()==null){
            newsMdbEo.setUpdateTime(new Date());
        }
        NewsMdbEo mdbEo = newsMdbDao.save(newsMdbEo);
        return mdbEo.getId();
    }

    @Override
    public NewsMdbEo findById(String id) {
        Optional<NewsMdbEo> newsMdbEo = newsMdbDao.findById(id);
        if(newsMdbEo.isPresent()){
            return newsMdbEo.get();
        }
        return null;
    }

    @Override
    public List<NewsMdbEo> findAll() {
        return newsMdbDao.findAll();
    }

    @Override
    public String update(NewsMdbEo newsMdbEo) {
        if(newsMdbEo.getCreateTime()==null){
            newsMdbEo.setCreateTime(new Date());
        }
        if(newsMdbEo.getUpdateTime()==null){
            newsMdbEo.setUpdateTime(new Date());
        }
        NewsMdbEo mdbEo = newsMdbDao.save(newsMdbEo);
        return mdbEo.getId();
    }

    @Override
    public Page<NewsMdbEo> findByPage(NewsMdbEo newsMdbEo, Pageable page) {
        Page<NewsMdbEo> mdbEoPage = newsMdbDao.findAll(page);
        return mdbEoPage;
    }
}
